<template>
	<view class="container">
		
		<view class="cd-bg">
			<!-- 基本信息 -->
			<view class="cd-title">
				<uni-row>
					<uni-col :span="20">
						<view class="param">基本信息</view>
					</uni-col>
					<uni-col :span="4">
						<view class="param" v-if="detail.sta==0&&detail.chexiao==0"><uni-tag text="审核" type="warning"></uni-tag></view>
						<view class="param" v-if="detail.sta==1&&detail.chexiao==0"><uni-tag text="通过" type="primary"></uni-tag></view>
						<view class="param" v-if="detail.sta==2&&detail.chexiao==0"><uni-tag text="拒绝" type="error"></uni-tag></view>
						<view class="param" v-if="detail.chexiao==1"><uni-tag text="撤销" type="default"></uni-tag></view>
					</uni-col>
				</uni-row>
			</view>
			<view class="cd-body">
				<view class="param"><b>申请人：</b>{{detail.shenqingname}}</view>
				<view class="param"><b>申请时间：</b>{{getFormatedTimeStr(detail.createTime)}}</view>
			</view>
			
			<!-- 特殊参数 -->
			<view class="cd-line">
				<view class="param">申请信息</view>
			</view>
			<view class="cd-body">
				<view class="param"><b>申请部门：</b>{{detail.department}}</view>
				<view class="param"><b>部门负责人：</b>{{detail.fuzerenname}}</view>
				<view class="param"><b>开始时间：</b>{{getFormatedTimeStr(detail.startTime, 'MM-DD') + (['', '上午', '下午'][detail.startWu || 0])}}</view>
				<view class="param"><b>结束时间：</b>{{getFormatedTimeStr(detail.endTime, 'MM-DD') + (['', '上午', '下午'][detail.endWu || 0])}}</view>
				<view class="param"><b>加班时长：</b>{{detail.shichang}}</view>
				<view class="param"><b>事由：</b>{{detail.reason}}</view>
				<view class="param"><b>加班人员</b></view>
				<uni-row v-for="(u,idx) in detail.attenders" :key="idx">
					<uni-col :span="12"><view class="param">工号：{{u.uid}}</view></uni-col>
					<uni-col :span="12"><view class="param">姓名：{{u.name}}</view></uni-col>
				</uni-row>
			</view>
		
	
			<!-- 审核进度 -->
			<view class="cd-line">
				<view class="param"><b>审核进度</b></view>
			</view>
			
			<view class="cd-body">
				<view v-for="(audi, idx,id) in detail.auditors" :key="idx">
					<uni-row>
						<uni-col :span="24">
							<view v-if="id>0" class="step-arrow">
								<uni-icons type="bottom" size="30" color='#a5eaff'></uni-icons>
							</view>
						</uni-col>
					</uni-row>
					<view class="step">
						<view class="step-title">
							<uni-row>
								<uni-col :span="8">
									<uni-row>
										<uni-col :span="12">
											<view class="step-title-left">{{audi.name}}</view>
										</uni-col>
										<uni-col :span="12">
											<view v-if="cuibanShow(id)" style="text-align: right; color: dodgerblue;" @click="cuiban(audi.uid)">
													<uni-icons type="paperplane-filled" color="dodgerblue" size="16"></uni-icons>催办
											</view>
										</uni-col>
									</uni-row>
								</uni-col>
								<uni-col :span="8">
									<view class="step-title-center">
										<uni-tag text="未审核" size="mini" type="default" v-if="audi.sta==0"></uni-tag>
										<uni-tag text="同意" size="mini" type="primary" v-if="audi.sta==1"></uni-tag>
										<uni-tag text="拒绝" size="mini" type="error" v-if="audi.sta==2"></uni-tag>
									</view>
								</uni-col>
								<uni-col :span="8">
									<view class="step-title-right">{{audi.stepName}}</view>
								</uni-col>
							</uni-row>
						</view>
						<view class="step-info">
							<uni-row>
								<uni-col :span="16" v-if="audi.sta>0 && audi.msg=='' ">
									<view class="msg"><b>意见：</b>无</view>
								</uni-col>
								<uni-col :span="16" v-if="audi.sta>0 && audi.msg!='' ">
									<view class="msg"><b>意见：</b>{{audi.msg}}</view>
								</uni-col>
								<uni-col :span="8" v-if="audi.sta>0">
									<view class="img-qm">
										<image :src="audi.signimg" />
									</view>
								</uni-col>
							</uni-row>
						</view>
						<view class="step-footer">
							<uni-row>
								<uni-col :span="24" v-if="audi.sta>0">
										<view class="step-footer-right">{{getFormatedTimeStr(audi.ts)}}</view>
								</uni-col>
							</uni-row>
						</view>
					</view>
				</view>
			</view>

			
			<!-- 承办进度 -->
			<!-- {{detail}} -->
			<view class="cd-line" v-if="detail.chengbanSta>0">
				<view class="param"><b>承办进度</b></view>
			</view>
			
			<view class="cd-body" v-if="detail.chengbanSta>0">
				<view v-for="(cbr, idx,id) in detail.chengbanren" :key="idx">
					<view v-if="id>0" style="padding: 10px;"></view>
					<view class="step">
						<view class="step-title-warn">
							<uni-row>
								<uni-col :span="8">
									<uni-row>
										<uni-col :span="12">
											<view class="step-title-left">{{cbr.name}}</view>
										</uni-col>
										<uni-col :span="12">
											<view v-if="cbr.sta==0" style="text-align: right; color: dodgerblue;" @click="cuiban(cbr.uid, '待完成')">
												<uni-icons type="paperplane-filled" color="dodgerblue" size="16"></uni-icons>催办
											</view>
										</uni-col>
									</uni-row>
									<!-- <view class="step-title-left">{{cbr.name}}</view> -->
								</uni-col>
								<uni-col :span="8">
									<view class="step-title-center">
										<uni-tag text="未完成" size="mini" type="default" v-if="cbr.sta==0"></uni-tag>
										<uni-tag text="完成" size="mini" type="primary" v-if="cbr.sta==1"></uni-tag>
									</view>
								</uni-col>
								<uni-col :span="8">
									<view class="step-title-right">{{cbr.positionName}}</view>
								</uni-col>
							</uni-row>
						</view>
						<view class="step-info">
							<uni-row>
								<uni-col :span="24" v-if="cbr.sta>0 && cbr.msg=='' ">
									<view class="msg"><b>回复：</b>无</view>
								</uni-col>
								<uni-col :span="24" v-if="cbr.sta>0 && cbr.msg!='' ">
									<view class="msg"><b>回复：</b>{{cbr.msg}}</view>
								</uni-col>
								
							</uni-row>
						</view>
						<view class="step-footer">
							<uni-row>
								<uni-col :span="24" v-if="cbr.sta>0">
										<view class="step-footer-right">{{getFormatedTimeStr(cbr.ts)}}</view>
								</uni-col>
							</uni-row>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 操作按钮 -->
			<view style="padding: 30px;"></view>
			<view class="cd-footer" v-if="see == 1 && isMyAgree() == 1">
				<button @click="clickAudit(0)" type="primary">审批</button>
			</view>
			<view class="cd-footer" v-if="see == 1 && isMyAgreeChengban() == 1">
				<button @click="clickAudit(1)" type="warn">承办</button>
			</view>
			<view class="cd-footer" v-if="detail.chexiao == 0 && detail.sta == 0 && detail.uid == userInfo.uid">
				<button @click="revert()" type="warn">撤销</button>
			</view>
		</view>
		
	</view>
</template>

<script>
import { websock } from '../../net/websock'
import moment from 'moment'
	export default {
		data() {
			return {
				id: 0,
				taskid: 0,
				see: 0,
				detail: {},
				userInfo: {},
			}
		},
		async onReady() {
			try {
				this.userInfo = await websock.getUserInfo()
			} catch (e) {
				uni.showToast({
					title: e.message
				})
			}
		},
		async onLoad(params) {
			this.id = params.id
			this.taskid = params.taskid
			this.see = params.see
			console.log('onload', params)
			
			this.getInfo()
		},
		methods: {
			async getInfo() {
				this.detail= await websock.jiabanApplyDetail({id: this.id})

				websock.getUserInfoByUid({uid: this.detail.uid}).then(res => {
					this.detail.shenqingname = res.name
				})

				websock.getUserInfoByUid({uid: this.detail.fuzeren}).then(res => {
					this.detail.fuzerenname = res.name
				})

				for (let key in this.detail.auditors) {
					websock.getUserInfoByUid({uid: this.detail.auditors[key].uid}).then(res => {
						this.detail.auditors[key].name = res.name
						this.detail.auditors[key].signimg = res.signimg
					})
				}
			},
			// 进行审批
			clickAudit(typ) {
				uni.navigateTo({
					url: `/pages/common/audit?taskid=${this.taskid}&typ=${typ}`
				})
			},
			// 判断我是不是审批过
			isMyAgree() {
				let myid = this.userInfo.uid
				for (let key in this.detail.auditors) {
					if (this.detail.auditors[key].uid == myid && this.detail.auditors[key].sta == 0) {
						return 1
					}
				}
				return 0
			},
			// 判断我是不是承办过
			isMyAgreeChengban() {
				let myid = this.userInfo.uid
			
				for (let key in this.detail.chengbanren) {
					if (this.detail.chengbanren[key].uid == myid && this.detail.chengbanren[key].sta == 0) {
						return 1
					}
				}
				return 0
			},
			// 获取格式化时间字符串
			getFormatedTimeStr(stamp, str='MM-DD HH:mm:ss') {
				return moment(stamp).format(str)
			},
			async revert() {
				try {
					await websock.revertJiabanApply({id: this.detail.id})
					uni.$emit('agree_task', this.detail.id)
					this.getInfo()
				} catch (err) {
					console.error(err)
				}
			},
			cuibanShow(idx) {
				console.log(idx)
				if (!this.detail.auditors) return false
				// 第一个 还没审核 可以催
				if (idx == 0) {
					if (this.detail.auditors['auditor' + idx].sta == 0) {
						return true
					}
				} else {
					// 不是第一个
					// 上一个审核通过了
					if (this.detail.auditors['auditor' + (idx - 1)].sta == 1) {
						// 这个还没审核
						if (this.detail.auditors['auditor' + idx].sta == 0) {
							return true
						}
					}
				}
				return false
			},
			async cuiban(uid, sta = '待审批') {
				uni.showModal({
					title: "提示",
					content: "是否催办？",
					success: async (res) => {
						if (res.confirm) {
							await websock.cuiban({uid: uid, processName: '加班申请', processStatus: sta})
							uni.showToast({ title: '催办成功' })
						}
					}
				});
			}
		}
	}
</script>

<style>
@import url("../common/detail.css");
</style>
